<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate(3)"
      >
        新增头像
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate(4)"
      >
        新增广告轮播图
      </el-button>
    </div>

    <el-table
      :key="key"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        prop="id"
        align="center"
        width="60"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="imgUrl"
        label="图片地址"
        align="center"
      />
      <el-table-column
        label="图片类型"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.type | typeFilter }}
        </template>
      </el-table-column>
      <el-table-column
        label="图片状态"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.state | stateFilter }}
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.created  }}
        </template>
      </el-table-column>
      <el-table-column
        prop="author"
        label="操作"
        align="center"
        min-width="160"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="updateImg(scope.row)"
          >修改图片</el-button>
          <el-button
            type="warning"
            size="mini"
            @click="change(scope.row)"
          >更改状态</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="delImg(scope.row)"
          >删除</el-button>
        </template>

      </el-table-column>

    </el-table>

    <el-dialog
      title="上传图片"
      :visible.sync="dialogFormVisible"
      top="15vh"
    >
      <addImgall
        :image-url="imageUrl"
        @imgUrl="getImg"
      />
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { getImgList } from '@/api/v1'
import { addRate, imgAdd } from '@/api/v1'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import addImgall from '@/components/img'
import { parseTime } from '@/utils'

export default {
  directives: { waves },
  components: { Pagination, addImgall },
  filters: {
    typeFilter(type) {
      if (type == 3) {
        return '头像'
      } else if (type == 4) {
        return '轮播图'
      } else {
        return ''
      }
    },
    stateFilter(state) {
      if (state == 0) {
        return '正常'
      } else if (state == 1) {
        return '禁用'
      } else {
        return ''
      }
    }
  },
  data() {
    return {
      imageUrl: '',
      img1: '',
      dialogFormVisible: false,
      dialogStatus: 'create',
      tableData: [],
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,

      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id',
        cooperationId: ''
      },
      data: {
        rate: '',
        remark: ''
      },
      key: 1, // table key
      temp: {
        id: '',
        cooperationId: '',
        imgUrl: '',
        state: 0,
        type: ''
      },

      rules: {
        rate: [{ required: true, message: '请输入折扣', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入折扣', trigger: 'blur' }]
      }
    }
  },
  watch: {
    checkboxVal(valArr) {
      this.formThead = this.formTheadOptions.filter(
        i => valArr.indexOf(i) >= 0
      )
      this.key = this.key + 1 // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
    }
  },
  created() {
    const id = this.$route.query.relationId
    this.listQuery.cooperationId = id
    this.temp.cooperationId = id
    console.log(id)
    this.getList()
  },
  methods: {
    getImg(data) {
      console.log(data)
      this.temp.imgUrl = data
      this.imageUrl = data
    },
    change(row) {
      this.$confirm('此操作将该图片显示状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.del(row)
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });
        })
    },
    delImg(row) {
      this.$confirm('此操作将删除该图片, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log(row)
          this.temp.imgUrl = row.imgUrl
          this.temp.id = row.id
          this.temp.type = row.type
          this.temp.state = 3

          imgAdd(this.temp).then(res => {
            this.dialogFormVisible = false
            this.$notify({
              title: '删除图片',
              message: res.msg,
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });
        })
    },
    del(row) {
      console.log(row)
      this.temp.imgUrl = row.imgUrl
      this.temp.id = row.id
      this.temp.type = row.type
      var state = row.state
      console.log(state)
      if (state == 0) {
        state = 1
      } else if (state == 1) {
        state = 0
      }
      console.log(state)
      this.temp.state = state

      imgAdd(this.temp).then(res => {
        this.dialogFormVisible = false
        this.$notify({
          title: '更改状态',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    updateImg(row) {
      this.dialogStatus = 'update'
      this.imageUrl = row.imgUrl
      this.temp.imgUrl = row.imgUrl
      this.temp.id = row.id
      this.temp.state = row.state
      this.temp.type = row.type
      this.dialogFormVisible = true
    },
    updateData(row) {
      console.log(row)
      imgAdd(this.temp).then(res => {
        this.dialogFormVisible = false
        this.$notify({
          title: '修改成功',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    createData() {
      console.log(this.temp)
      imgAdd(this.temp).then(res => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    handleCreate(type) {
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
      this.imageUrl = ''
      this.temp.type = type
    },

    getList() {
      this.listLoading = true
      getImgList(this.listQuery).then(res => {
        this.tableData = res.data
        this.total = Number(res.data.total)

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>

